//
//  CZEmotionTipView.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/30.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//
import UIKit
import pop

class CZEmotionTipView: UIImageView {
    
    private var preEmotion: CZEmotionModel?
    
    var emotion: CZEmotionModel? {
        didSet {
            if emotion == preEmotion {return}
            
            preEmotion = emotion
            
            tipButton.setTitle(emotion?.emoji, for: [])
            tipButton.setImage(emotion?.image, for: [])
            
            let anim = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
            anim?.toValue = 8
            anim?.fromValue = 30
            anim?.springBounciness = 20
            
            tipButton.layer.pop_add(anim, forKey: nil)
        }
    }

    private lazy var tipButton = UIButton()
    
    init() {
        let bundle = CZEmotionManager.shared.bundle
        let image = UIImage(named: "other/emoticon_keyboard_magnifier", in: bundle, compatibleWith: nil)
        
        super.init(image: image)
        
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)
        
        tipButton.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        tipButton.frame = CGRect(x: 0, y: 8, width: 36, height: 36)
        tipButton.center.x = bounds.width * 0.5
        
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        addSubview(tipButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
